#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QFormLayout>
#include "QAction"
#include "QMessageBox"
#include <QKeyEvent>
#include "QDebug"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    mylayout();

}
void MainWindow::mylayout()
{
    QWidget *centerWindow = new QWidget;
    this->setCentralWidget(centerWindow);

    label1 = new QLabel("显示");
    label2 = new QLabel("结果");
    QHBoxLayout *h1 = new QHBoxLayout;
    h1->addWidget(label1);
    h1->addWidget(label2);
    label1->setMaximumHeight(15);
    label2->setMaximumHeight(15);
    h1->setStretch(0,2);
    h1->setStretch(1,1);

    showEdit = new QTextEdit;
    resultEdit = new QTextEdit;
    QHBoxLayout *h2 =new QHBoxLayout;
    h2->addWidget(showEdit);
    h2->addWidget(resultEdit);
    showEdit->setMaximumHeight(30);
    resultEdit->setMaximumHeight(30);

    showEdit->setReadOnly(true);
    resultEdit->setReadOnly(true);
    h2->setStretch(0,2);
    h2->setStretch(1,1);

    //backBt = new QPushButton("Back");
    //backBt = new QPushButton(" ");
    removeBt = new QPushButton("remove");
    clearBt = new QPushButton("clear");
    sumBt = new QPushButton("+");
    //backBt->setMinimumHeight(50);
    removeBt->setMinimumHeight(50);
    clearBt->setMinimumHeight(50);
    clearBt->setMaximumWidth(190);
    sumBt->setMinimumHeight(50);
    sumBt->setMaximumWidth(190);
    QHBoxLayout *h3 = new QHBoxLayout;
    //h3->addWidget(backBt);
    h3->addWidget(removeBt);
    h3->addWidget(clearBt);
    h3->addWidget(sumBt);

    sevenBt = new QPushButton("7");
    eightBt = new QPushButton("8");
    nineBt = new QPushButton("9");
    decBt = new QPushButton("-");
    sevenBt->setMinimumHeight(50);
    eightBt->setMinimumHeight(50);
    nineBt->setMinimumHeight(50);
    decBt->setMinimumHeight(50);
    QHBoxLayout *h4= new QHBoxLayout;
    h4->addWidget(sevenBt);
    h4->addWidget(eightBt);
    h4->addWidget(nineBt);
    h4->addWidget(decBt);

    fourBt = new QPushButton("4");
    fiveBt = new QPushButton("5");
    sixBt = new QPushButton("6");
    mulBt = new QPushButton("*");
    fourBt->setMinimumHeight(50);
    fiveBt->setMinimumHeight(50);
    sixBt->setMinimumHeight(50);
    mulBt->setMinimumHeight(50);
    QHBoxLayout *h5= new QHBoxLayout;
    h5->addWidget(fourBt);
    h5->addWidget(fiveBt);
    h5->addWidget(sixBt);
    h5->addWidget(mulBt);

    oneBt = new QPushButton("1");
    twoBt = new QPushButton("2");
    threeBt = new QPushButton("3");
    divBt = new QPushButton("/");
    oneBt->setMinimumHeight(50);
    twoBt->setMinimumHeight(50);
    threeBt->setMinimumHeight(50);
    divBt->setMinimumHeight(50);
    QHBoxLayout *h6= new QHBoxLayout;
    h6->addWidget(oneBt);
    h6->addWidget(twoBt);
    h6->addWidget(threeBt);
    h6->addWidget(divBt);

    //_Bt = new QPushButton("+/-");
    //_Bt = new QPushButton(" ");
    zeroBt = new QPushButton("0");
    pointBt = new QPushButton(".");
    //const QSize BUTTON_SIZE = QSize(10, 50);
    equalBt = new QPushButton("=");
    //_Bt->setMinimumHeight(50);
    zeroBt->setMinimumHeight(50);
    zeroBt->setMaximumWidth(190);
    pointBt->setMinimumHeight(50);
    pointBt->setMaximumWidth(190);
    //pointBt->resize(BUTTON_SIZE);
    equalBt->setMinimumHeight(50);
    QHBoxLayout *h7= new QHBoxLayout;
    h7->addWidget(pointBt);
    h7->addWidget(zeroBt);
    //h7->addWidget(_Bt);
    h7->addWidget(equalBt);


    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addLayout(h1);
    mainLayout->addLayout(h2);
    mainLayout->addLayout(h3);
    mainLayout->addLayout(h4);
    mainLayout->addLayout(h5);
    mainLayout->addLayout(h6);
    mainLayout->addLayout(h7);

    centerWindow->setLayout(mainLayout);

    //connect(backBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtBack()));
    connect(removeBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtRemove()));
    connect(clearBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtClear()));
    connect(sumBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtSum()));
    connect(sevenBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtSeven()));
    connect(eightBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtEight()));
    connect(nineBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtNine()));
    connect(decBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtDec()));
    connect(fourBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtFour()));
    connect(fiveBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtFive()));
    connect(sixBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtSix()));
    connect(mulBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtMul()));
    connect(oneBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtOne()));
    connect(twoBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtTwo()));
    connect(threeBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtThree()));
    connect(divBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtDiv()));
    //connect(_Bt,SIGNAL(clicked(bool)),this,SLOT(onPushBtSign()));
    connect(zeroBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtZero()));
    connect(pointBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtPoint()));
    connect(equalBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtEqual()));

}


void MainWindow::BeforeOperator(int iOperator)
{
    if(contextStr.size() == 0 || contextStr == "-")
    {
        return;
    }
    if(operat == 0)
    {
        firstOpeNum = contextStr.toDouble();
    }
    operat = iOperator;
    contextStr.clear();
    showEdit->setText(contextStr);
}


void MainWindow::onPushBtOne()
{
    JudgeZero();
    contextStr = contextStr + "1";
    showEdit->setText(contextStr);
}

void MainWindow::onPushBtTwo()
{
    JudgeZero();
    contextStr = contextStr + "2";
    showEdit->setText(contextStr);
}

void MainWindow::onPushBtThree()
{
    JudgeZero();
    contextStr = contextStr + "3";
    showEdit->setText(contextStr);
}

void MainWindow::onPushBtFour()
{
    JudgeZero();
    contextStr = contextStr + "4";
    showEdit->setText(contextStr);
}

void MainWindow::onPushBtFive()
{
    JudgeZero();
    contextStr = contextStr + "5";
    showEdit->setText(contextStr);
}

void MainWindow::onPushBtSix()
{
    JudgeZero();
    contextStr = contextStr + "6";
    showEdit->setText(contextStr);
}

void MainWindow::onPushBtSeven()
{
    JudgeZero();
    contextStr = contextStr + "7";
    showEdit->setText(contextStr);
}

void MainWindow::onPushBtEight()
{
    JudgeZero();
    contextStr = contextStr + "8";
    showEdit->setText(contextStr);
}

void MainWindow::onPushBtNine()
{
    JudgeZero();
    contextStr = contextStr + "9";
    showEdit->setText(contextStr);
}

void MainWindow::onPushBtZero()
{
    JudgeZero();
    contextStr = contextStr + "0";
    showEdit->setText(contextStr);
}
#if 0
void MainWindow::onPushBtBack()
{

}
#endif

void MainWindow::onPushBtRemove()
{
    edittext = contextStr.toInt();
    edittext = edittext / 10;
    contextStr = QString::number(edittext);
    showEdit->setText(contextStr);
}

void MainWindow::onPushBtClear()
{
    contextStr.clear();
    showEdit->setText(contextStr);
    resultEdit->setText("0");
}

void MainWindow::onPushBtSum()
{
    BeforeOperator(ADD);
}

void MainWindow::onPushBtDec()
{
    BeforeOperator(DEC);
}

void MainWindow::onPushBtMul()
{
    BeforeOperator(MUL);
}

void MainWindow::onPushBtDiv()
{
    BeforeOperator(DIV);
}
#if 0
void MainWindow::onPushBtSign()
{

}
#endif

void MainWindow::onPushBtPoint()
{
    JudgeZero();
    contextStr = contextStr + ".";
    showEdit->setText(contextStr);
}

void MainWindow::onPushBtEqual()
{
    QString strTem;
    if(contextStr.size() == 0 || contextStr == "-")
    {
        return;
    }
    secondOpeNum = contextStr.toDouble();
    int const add = 1;
    int const dec = 2;
    int const mul = 3;
    int const div = 4;
    switch (operat) {
        case 0:
        break;
        case add:
        {
            result = secondOpeNum + firstOpeNum;
            strTem = QString::number(result);
            resultEdit->setText(strTem);
        }
        break;
        case dec:
        {
            result = firstOpeNum - secondOpeNum;
            strTem = QString::number(result);
            resultEdit->setText(strTem);
        }
        break;
        case mul:
        {
            result = secondOpeNum * firstOpeNum;
            strTem = QString::number(result);
            resultEdit->setText(strTem);
        }
        break;
        case div:
        {
            if(secondOpeNum*2 == 0){
                resultEdit->setText("error");
                return;
            }
            result = firstOpeNum / secondOpeNum;
            strTem = QString::number(result);
            resultEdit->setText(strTem);
        }
        break;
    }
    init();
    contextStr.clear();
    showEdit->setText(contextStr);

}


void MainWindow::JudgeZero()
{
    if(contextStr.size()==1)
    {
        if(contextStr.contains("0"))
        {
            contextStr.clear();
        }
    }
}

void MainWindow::init()
{
    firstOpeNum = 0;
    secondOpeNum = 0;
    operat = 0;
}


void MainWindow::keyPressEvent(QKeyEvent * e)
{
    int iKey = e->key();
    switch(iKey)
    {
        case Qt::Key_0:onPushBtZero();break;
        case Qt::Key_1:onPushBtOne();break;
        case Qt::Key_2:onPushBtTwo();break;
        case Qt::Key_3:onPushBtThree();break;
        case Qt::Key_4:onPushBtFour();break;
        case Qt::Key_5:onPushBtFive();break;
        case Qt::Key_6:onPushBtSix();break;
        case Qt::Key_7:onPushBtSeven();break;
        case Qt::Key_8:onPushBtEight();break;
        case Qt::Key_9:onPushBtNine();break;
        //case Qt::Key_Backspace:onPushBtBack();break;
        case Qt::Key_Equal:onPushBtEqual();break;
        case Qt::Key_Plus:onPushBtSum();break;
        case Qt::Key_Period:onPushBtPoint();break;
        case Qt::Key_Minus:onPushBtDec();break;
        case Qt::Key_Asterisk:onPushBtMul();break;
        case Qt::Key_Slash:onPushBtDiv();break;
    }
}


MainWindow::~MainWindow()
{
    delete ui;
}

